package com.aibili.pojo.model;

public class PageModel {

	private long count; // 总条数
	private int pageIndex; // 起始页
	private int beginIndex; // 起始位置
	private int pageSize; // 每页数量
	private long pageCount; // 总页数
	private boolean hasNextpage; // 有无起始页
	private boolean hasPreviouspage; // 有无结束页

	/**
	 * 
	 * @Description 生成分页
	 * @param count
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	public static PageModel createPage(long count, int pageIndex, int pageSize) {
		pageIndex = getPageIndex(pageIndex);
		pageSize = getPageSize(pageSize);
		int beginIndex = getBeginIndex(pageIndex, pageSize);
		long pageCount = getPageCount(count, pageSize);
		if (beginIndex >= count) { // 如果起始位置 > 总条数
			if (pageIndex > 1) { // 如果不是第一页
				pageIndex = (int) pageCount; // 把查询的页数改成最大的页数
			}
			beginIndex = getBeginIndex(pageIndex, pageSize);
		}
		boolean hasPreviouspage = isFirstPage(pageIndex);
		boolean hasNextpage = isLastPage(pageIndex, pageCount);
		return new PageModel(count, pageIndex, beginIndex, pageSize, pageCount, hasNextpage, hasPreviouspage);
	}

	public static int getPageIndex(int pageIndex) {
		return pageIndex == 0 ? 1 : pageIndex;
	}

	public static int getPageSize(int pageSize) {
		return pageSize == 0 ? 10 : pageSize;
	}

	public static long getPageCount(long count, int pageSize) {
		long pageCount = 0;
		if (count != 0 && pageSize != 0) {
			if (count % pageSize == 0) {
				pageCount = count / pageSize;
			} else {
				pageCount = count / pageSize + 1;
			}
		}
		return pageCount;
	}

	public static boolean isFirstPage(int pageIndex) {
		return pageIndex == 1 ? false : true;
	}

	public static boolean isLastPage(int pageIndex, long pageCount) {
		return pageIndex == pageCount || pageCount == 0 ? false : true;
	}

	public static int getBeginIndex(int pageIndex, int pageSize) {
		return (pageIndex - 1) * pageSize;
	}

	public PageModel() {
		super();
	}

	public PageModel(long count, int pageIndex, int beginIndex, int pageSize, long pageCount, boolean hasNextpage,
			boolean hasPreviouspage) {
		super();
		this.count = count;
		this.pageIndex = pageIndex;
		this.beginIndex = beginIndex;
		this.pageSize = pageSize;
		this.pageCount = pageCount;
		this.hasNextpage = hasNextpage;
		this.hasPreviouspage = hasPreviouspage;
	}

	public long getCount() {
		return count;
	}

	public void setCount(long count) {
		this.count = count;
	}

	public long getPageCount() {
		return pageCount;
	}

	public void setPageCount(long pageCount) {
		this.pageCount = pageCount;
	}

	public int getPageIndex() {
		return pageIndex;
	}

	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	public int getBeginIndex() {
		return beginIndex;
	}

	public void setBeginIndex(int beginIndex) {
		this.beginIndex = beginIndex;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public boolean isHasNextpage() {
		return hasNextpage;
	}

	public void setHasNextpage(boolean hasNextpage) {
		this.hasNextpage = hasNextpage;
	}

	public boolean isHasPreviouspage() {
		return hasPreviouspage;
	}

	public void setHasPreviouspage(boolean hasPreviouspage) {
		this.hasPreviouspage = hasPreviouspage;
	}

}
